package com.um.jdy.platform.free.manager;

import com.um.jdy.common.charge.entity.dto.FreeUserDTO;
import com.um.jdy.common.charge.entity.po.FreeUser;
import com.um.jdy.common.charge.entity.vo.FreeUserVO;
import com.um.jdy.common.charge.service.FreeUserService;
import com.um.jdy.common.device.code.DeviceCode;
import com.um.jdy.common.device.service.StationService;
import com.um.jdy.common.member.code.UserCode;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.platform.free.entity.vo.FreeUserListVO;
import com.um.jdy.platform.user.service.SessionUserService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseCMSManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class FreeUserManager extends BaseCMSManager<FreeUserService, FreeUser, FreeUserDTO, FreeUserVO> {


    @Autowired
    SessionUserService sessionUserService;

    @Autowired
    MemberUserService memberUserService;

    @Autowired
    StationService stationService;


    public Response<Page<FreeUserListVO>> getFeePage(PageParams params) {

        Page<FreeUserListVO> page = new Page<>();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());
        params.getCondition().put("f.com_id", getComId());

        page = service.getCMSViewModelPageBySqlName(page, "get_free_user_page", "get_free_user_count", FreeUserListVO.class, params.getProperties(), params.getValues(), params.getRangeEntitys(), params.getOrderNames(), params.getSortOns());

        return Response.success(page);
    }


    public Response<FreeUser> addEntity(FreeUserDTO dto) {
        if(memberUserService.getEntity(dto.getMemberUserId()) == null){
            return Response.failed(UserCode.NoUser);
        }

        if(stationService.getEntity(dto.getDeviceStationId()) == null){
            return Response.failed(DeviceCode.NoStation);
        }

        FreeUser entity = UMBeanUtils.copyBean(dto, FreeUser.class);
        entity.setCreateTime(new Date());
        entity.setCreateUserId(sessionUserService.getTokenUserId());
        entity.setType("admin");
        return Response.success(this.service.addEntity(entity));
    }



    public Response<FreeUser> updateEntity(String id, FreeUserDTO dto) {
        if(memberUserService.getEntity(dto.getMemberUserId()) == null){
            return Response.failed(UserCode.NoUser);
        }

        if(stationService.getEntity(dto.getDeviceStationId()) == null){
            return Response.failed(DeviceCode.NoStation);
        }


        FreeUser entity = service.getEntity(id);
        BeanUtils.copyProperties(dto, entity);
        entity.setDevicePileId(StringUtil.getTrim(dto.getDevicePileId()));
        return Response.success(this.service.updateEntity(entity));
    }

}
